package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.data.Result;
import com.dk.data.ResultEnum;
import com.dk.pojo.comment.Comment;
import com.dk.pojo.user.TbUser;
import com.dk.service.comment.CommentService;
import com.dk.utils.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@CrossOrigin
@Controller
@RequestMapping("comment")
public class CommentController {
    @Reference
    private CommentService commentService;

    private final Logger logger = LoggerFactory.getLogger(CommentController.class);

    @RequestMapping("saveComment")
    @ResponseBody
    public Result saveComment(Comment comment) {
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        try {
            int i = commentService.saveComment(comment,user);
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("保存评论错误");
            return Result.response(ResultEnum.FAIL);
        }
    }
    @RequestMapping("getComment")
    @ResponseBody
    public Result getComment(@RequestParam(defaultValue = "1") Integer pageNumber,Long goodsId) {
        try {
            PageInfo<Comment> pageInfo = commentService.getComment(pageNumber,goodsId);
            return Result.response(ResultEnum.OK).setData(pageInfo);
        } catch (Exception e) {
            logger.error("获取comment错误");
            return Result.response(ResultEnum.FAIL);
        }
    }

}
